//把obj对象里的值覆盖到newobj里面
function deepCopy(newobj, obj) {
  if (typeof obj != 'object') {
    return obj;
  }
  for (var attr in obj) {
    var a = {};
    if (newobj[attr]) {
      a = newobj[attr];
    }
    newobj[attr] = deepCopy(a, obj[attr]);
  }
  return newobj;
}

//操作成功后，的提示信息
function successToShow(msg = '保存成功', callback = function () {}) {
  uni.showToast({
    title: msg,
    icon: 'success',
    duration: 2000,
  });
  setTimeout(function () {
    callback();
  }, 500);
}

//操作失败的提示信息
function errorToShow(msg = '操作失败', callback = function () {}) {
  uni.showToast({
    title: msg,
    icon: 'none',
    duration: 2000,
  });
  setTimeout(function () {
    callback();
  }, 2000);
}

//消息提示框
function msgToShow(msg) {
  uni.showToast({
    title: msg,
    icon: 'none',
    position: 'center',
	duration: 2000,
  });
}

//加载显示
function loadToShow(msg = '加载中') {
  uni.showToast({
    title: msg,
    icon: 'loading',
    duration: 30000
  });
}

//加载隐藏
function loadToHide() {
  uni.hideToast();
}

// loading动画
function showLoading(msg="加载中") {
	  uni.showLoading({
    title: msg
  });
}
// 关闭loading动画
function hideLoading() {
	  uni.hideLoading();
}


// 提示框
function modelShow(title = '提示', content = '确认执行此操作吗?', callback = () => {}, showCancel = true, cancelText = '取消',
  confirmText = '确定') {
  uni.showModal({
    title: title,
    content: content,
    showCancel: showCancel,
    cancelText: cancelText,
    confirmText: confirmText,
    cancelText: cancelText,
    success: function (res) {
      if (res.confirm) {
        // 用户点击确定操作
        setTimeout(() => {
          callback()
        }, 500)
      } else if (res.cancel) {
        // 用户取消操作
      }
    }
  });
}

//不足位数前面补0
function PrefixInteger(num, length) {
  return (Array(length).join('0') + num).slice(-length);
}

//验证是否是手机号
function isPhoneNumber(str) {
  var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (!myreg.test(str)) {
    return false;
  } else {
    return true;
  }
}

/**
 * 
 * 对象参数转为url参数
 * 
 */
function builderUrlParams(url, data) {
  if (typeof (url) == 'undefined' || url == null || url == '') {
    return '';
  }
  if (typeof (data) == 'undefined' || data == null || typeof (data) != 'object') {
    return '';
  }
  url += (url.indexOf("?") != -1) ? "" : "?";
  for (var k in data) {
    url += ((url.indexOf("=") != -1) ? "&" : "") + k + "=" + encodeURI(data[k]);
  }
  return url;
}

/**
 * 使用循环的方式判断一个元素是否存在于一个数组中
 * @param {Object} arr 数组
 * @param {Object} value 元素值
 */
function isInArray(arr, value) {
  for (var i = 0; i < arr.length; i++) {
    if (value === arr[i]) {
      return true;
    }
  }
  return false;
}

/**
 * 统一跳转
 */
function navigateTo(url) {
  uni.navigateTo({
    url: url,
    animationType: 'slide-in-right',
    animationDuration: 500
  });
}

/**
 *  关闭当前页面并跳转
 */
function redirectTo(url) {
  uni.redirectTo({
    url: url,
    animationType: 'slide-in-right',
    animationDuration: 500
  })
}

function navigateBack(url) {
  uni.navigateBack({
    url: url,
    animationType: 'slide-out-right',
    animationDuration: 350
  })
}

export {
  deepCopy,
  successToShow,
  errorToShow,
  loadToShow,
  msgToShow,
  loadToHide,
  PrefixInteger,
  isPhoneNumber,
  isInArray,
  navigateTo,
  redirectTo,
  navigateBack,
  modelShow,
  builderUrlParams,
	showLoading,
	hideLoading
}